package com.yiyi.credit.manage.abutment.ccxiapi.common.handler;


import com.yiyi.credit.manage.abutment.common.bean.APIResponse;
import com.yiyi.credit.manage.abutment.common.enums.ResponseCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.MessageFormat;


@ResponseBody
@ControllerAdvice
class GlobalExceptionHandler {

    private final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(Exception.class)
    public Object handleException(Exception ex, HttpServletRequest request, HttpServletResponse response) {
        writeLog(ex, request);
        return new APIResponse<>(ResponseCode.SERVICE_ERR);
    }

    private void writeLog(Exception ex, HttpServletRequest request) {
        String url = MessageFormat.format("Exception :{0}?{1}", request.getRequestURL(), request.getQueryString());
        log.error(url, ex);
    }
}
